<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-house__housekey}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <!-- <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        -->

        <el-row>
          <el-col :span="19">
            <el-form-item>
              日期：
              <el-date-picker
                v-model="dataForm.createDates"
                type="date"
                placeholder="开始时间"
                value-format="yyyy-MM-dd"
                style="width:136px"
              ></el-date-picker>
              {{' -'}}
              <el-date-picker
                v-model="dataForm.createDatee"
                type="date"
                placeholder="结束时间"
                value-format="yyyy-MM-dd"
                style="width:136px"
              ></el-date-picker>
            </el-form-item>

            <el-form-item>
              部门：
              <el-cascader
                class="FormSelect"
                v-model="deptSelectVal"
                :options="this.$store.state.DeptList"
                :show-all-levels="false"
                @change="deptIdChange"
                :props="deptProps"
                ref="myCascader"
                clearable
              ></el-cascader>-
              <el-select v-model="dataForm.userId" placeholder="人员" class="FormSelect" clearable>
                <el-option
                  v-for="item in maintainList"
                  :key="item.id"
                  :label="item.realName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              审核：
              <ren-select
                v-model="dataForm.examine"
                dict-type="examine"
                placeholder="审核类型"
                class="FormSelect"
              ></ren-select>
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="HousingID?$hasPermission('house:housekey:save')&&hasPermissionInfo('house:housekey:save'):$hasPermission('house:housekey:save')"
                type="primary"
                @click="addOrUpdateHandle()"
              >{{ $t('add') }}</el-button>
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="HousingID?$hasPermission('house:housekey:delete')&&hasPermissionInfo('house:housekey:delete'):$hasPermission('house:housekey:delete')"
                type="danger"
                @click="deleteHandle()"
              >删除钥匙</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            搜索：
            <el-input
              placeholder="请输入内容"
              v-model="dataForm.search"
              class="input-with-select"
              @keyup.enter.native="getDataList"
              clearable
              style="width:70%"
            >
              <el-button slot="append" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%;"
      >
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
        <el-table-column prop="houseId" label="房源id" header-align="center" align="center">
          <template slot-scope="scope">{{"FY - "+scope.row.id.substring(scope.row.id.length-6)}}</template>
        </el-table-column>
        <el-table-column prop="dictName" label="房间号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="number" label="钥匙编号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="address" label="钥匙位置" header-align="center" align="center"></el-table-column>
        <el-table-column prop="accessControl" label="是否有门禁" header-align="center" align="center"></el-table-column>
        <el-table-column prop="password" label="门禁密码" header-align="center" align="center"></el-table-column>
        <el-table-column prop="remark" label="备注" header-align="center" align="center"></el-table-column>
        <el-table-column prop="userName" label="收钥人" header-align="center" align="center"></el-table-column>
        <el-table-column prop="deptName" label="收钥部门" header-align="center" align="center"></el-table-column>
        <el-table-column prop="keyDate" label="收钥时间" header-align="center" align="center"></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              v-if="HousingID?$hasPermission('house:housekey:update')&&hasPermissionInfo('house:housekey:update'):$hasPermission('house:housekey:update')"
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
            >{{ $t('update') }}</el-button>
            <el-button
              v-if="HousingID?$hasPermission('house:housekey:delete')&&hasPermissionInfo('house:housekey:delete'):$hasPermission('house:housekey:delete')"
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
            >{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paginationBox">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        ></el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
        :houseId="HousingID"
      ></add-or-update>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./housekey-add-or-update";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/house/housekey/page",
        getDataListIsPage: true,
        exportURL: "/house/housekey/export",
        deleteURL: "/house/housekey",
        deleteIsBatch: true
      },
      dataForm: {
        id: "",
        examine: "",
        createDates: "",
        createDatee: "",
        deptId: "", //部门
        userId: this.$route.query.userId ? this.$route.query.userId : "",
        search: "",
        houseId: this.HousingID
      },
      deptSelectVal: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      maintainList: this.$store.state.maintainList
    };
  },
  props: ["HousingID", "permissionListDTO"],
  components: {
    AddOrUpdate
  },
  methods: {
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    //判断操作权限
    hasPermissionInfo(val) {
      let _this = this;
      let arr = _this.permissionListDTO;
      if (arr && arr != []) {
        for (let i = 0; i < arr.length; i++) {
          const element = arr[i];
          if (element.permission.indexOf(val) != -1 && element.state == true) {
            return true;
          }
        }
      } else {
        return false;
      }
    }
  },
  watch: {
    dataForm: {
      handler(nowVal, oldVal) {
        this.getDataList();
      },
      deep: true
    },
    HousingID: {
      handler(newValue, oldValue) {
        this.dataForm.houseId = newValue;
        this.query();
      }
    },
    $route(to, from) {
      if (to.name == "house-page-housekey") {
        if (this.$route.query.userId) {
          this.dataForm.userId = this.$route.query.userId;
        }
      }
    }
  }
};
</script>
